const Koa = require('koa');
// const http = require('http');
const Router = require('koa-router')
// const app = new Koa();
// const IO = require( 'koa-socket' )
// const io = IO.listen(server);
const fs = require('fs')
const koaBody = require('koa-body');

// const port = 12306


const app = new Koa()
const router = new Router()
// const server = http.createServer(app.callback());

// 初始化 socket
// const io = require("socket.io")(server, { cors: true });
// 监听

export function createServer(port: number) {

    router.get(`/`, async (ctx) => {
        // console.log('home')
        // ctx.body = 'home';
        ctx.body = fs.readFileSync('index.html', 'utf8')
        // ctx.response.body = 'linxot push demo'
    })
    
    router.post(`/`, async (ctx) => {
    
        // console.log('home2', ctx.request.body)
        // ctx.body = 'home';
        const { text } = ctx.request.body
        if (text) {
            // console.log('receive text:')
            console.log(text)
        }
        ctx.body = fs.readFileSync('index.html', 'utf8')
        // ctx.response.body = 'linxot push demo'
    })
    
    router.get(`/haha`, async (ctx) => {
        console.log('haha')
        ctx.body = 'haha';
        // ctx.response.body = 'haha'
    })
    
    
    app.use(koaBody())
    app.use(router.routes())
    
    // app.use(async ctx => {
    //   ctx.body = 'Hello World';
    // });
    
    app.listen(port)
    
    
    // console.log('ok')
}
